/**
 * Created by zhangyi on 2017/11/15.
 */
mainApp.factory("PEFactory",function() {
    var service={};

    //修改时获取数据
    var object;
    service.setPerEval=function(data){
        object=data;

    }
    service.getPerEval=function(){
        return object;
    }

    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list="";
        RequestUtils.postJson("../pe/get",obj)
            .done(function(resp){
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("获取列表","成功");
                }else{
                    CommonUtils.buildFailHint("获取列表","失败");
                }
            });
        return list;
    }

    //新增
    service.addPerEavlData=function(perEvalObj){
        RequestUtils.postForm("../pe/add",perEvalObj)
            .done(function(resp){
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint("add","成功");
                }else{
                    CommonUtils.buildFailHint("add","失败");
                }
            });
    }

    //据id查询
    service.selectById=function(id){
        var list="";
        RequestUtils.getJson("../pe/get/"+id)
            .done(function(resp){
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("查询","成功");
                }else{
                    CommonUtils.buildFailHint("查询","失败");
                }
            });
        return list;
    }

    //据工号或状态查询
    service.selectByJobnumOrStatus=function(dataWhere,currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        if(dataWhere.jobnum==null || dataWhere.jobnum==""){
            dataWhere.jobnum="null";
        }
        if(dataWhere.status!="0" || dataWhere.status!="1"){
            dataWhere.status=2;//为空
        }

        console.log(dataWhere)
        var list="";
        RequestUtils.postJson("../pe/getByJobnumOrState/"+dataWhere.jobnum+"/"+dataWhere.status,obj)
            .done(function(resp){
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("查询","成功");
                }else{
                    CommonUtils.buildFailHint("查询","失败");
                }
            });
        dataWhere.jobnum="";
        return list;
    }

    //修改
    service.updatePerEval=function(perEvalObj){
        RequestUtils.postForm("../pe/update",perEvalObj)
            .done(function(resp){
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint("update","成功");
                }else{
                    CommonUtils.buildFailHint("update","失败");
                }
            });
    }

    //删除
    service.delete=function(id){
        RequestUtils.getJson("../pe/delete/"+id)
            .done(function(resp){
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint("delete","成功");
                    location.reload(true);
                }else{
                    CommonUtils.buildFailHint("delete","失败");
                }
            });
    }

    return service;
});

mainApp.controller("PEListController",function($scope,$http,PEFactory,$state,overallSituationService) {
    overallSituationService.loginIntercept();

    $scope.PerEvalList=PEFactory.getListData(1);
    $scope.list=$scope.PerEvalList.items;

    //根据id查询
    $scope.selectPerEval=function(id){
        var perEval=PEFactory.selectById(id);
        PEFactory.setPerEval(perEval);

        var tab={url:"performance-evaluation-update",text:"绩效修改",id:"performance-evaluation-update-tab",closeable:1};
        $scope.$emit('addTab', tab);
        $state.go("performance-evaluation-update");
    };

    $scope.addTab=function(){
        var tab={url:"performance-evaluation-add",text:"绩效新增",id:"performance-evaluation-add-tab",closeable:1};
        $scope.$emit('addTab', tab);
    };

    //条件查询
    $scope.selectWhere=function(whereObj){
        $scope.listShow=false;
        $scope.listByWhereShow=true;

        $scope.PerEvalList=PEFactory.selectByJobnumOrStatus(whereObj,1);
        $scope.list=$scope.PerEvalList.items;
    };

    //删除
    $scope.delete=function(id){
        PEFactory.delete(id);
    };

    $scope.listShow=true;
    $scope.listByWhereShow=false;

    //翻页
    $scope.NextListData=function(pageIndex){
        if($scope.listByWhereShow){
            $scope.PerEvalList=PEFactory.selectByJobnumOrStatus($scope.perEvalWhere,pageIndex);
        }else{
            $scope.PerEvalList=PEFactory.getListData(pageIndex);
        }
        $scope.list=$scope.PerEvalList.items;
    }
});

mainApp.controller("PEAddController",function($scope,$http,PEFactory,empFactory) {
    //查询所有员工
    $scope.empList=empFactory.selectAll();
    console.log($scope.empList);

    $scope.addPerEavl=function(perEavl,empObj,res){
        var perEvalObj={
            "employee.id":empObj.id,
            "date":perEavl.date,
            "task_target_name":perEavl.task_target_name,
            "task_target_num":perEavl.task_target_num,
            "actual_complete_num":perEavl.actual_complete_num,
            "actual_complete_rate":perEavl.actual_complete_rate,
            "keytask":perEavl.keytask,
            "actual_proce":perEavl.actual_proce,
            "person_devolop_target":perEavl.person_devolop_target,
            "person_review":perEavl.person_review,
            "person_advise":perEavl.person_advise,
            "org_manage_target":perEavl.org_manage_target,
            "org_review":perEavl.org_review,
            "org_advise":perEavl.org_advise,
            "reward_name":perEavl.reward_name,
            "reward_reason":perEavl.reward_reason,
            "cash_type":perEavl.cash_type,
            "punish_name":perEavl.punish_name,
            "punish_reason":perEavl.punish_reason,
            "other_chat":perEavl.other_chat,
            "onwork_pendate":perEavl.onwork_pendate,
            "next_pendate":perEavl.next_pendate,
            "deal_status":res.value
        }
        if(empObj.id!=null&&empObj.id!=""
            && perEavl.task_target_name!=null&&perEavl.task_target_name!=""
            && perEavl.actual_complete_num!=null&&perEavl.actual_complete_num!=""
            && perEavl.actual_complete_rate!=null&&perEavl.actual_complete_rate!=""
            && perEavl.keytask!=null&&perEavl.keytask!=""
            && perEavl.actual_proce!=null&&perEavl.actual_proce!=""
            && perEavl.onwork_pendate!=null&&perEavl.onwork_pendate!=""){
            PEFactory.addPerEavlData(perEvalObj);
        }
    }

    $scope.results=[
        {name:"未处理",value:"0"},
        {name:"已完成",value:"1"}
    ];

    CommonUtils.setDatetimepicker("oneDatetimepicker");
    CommonUtils.setDatetimepicker("twoDatetimepicker");
    CommonUtils.setDatetimepicker("threeDatetimepicker");
});

mainApp.controller("PEUpdateController",function($scope,$http,PEFactory,empFactory) {
    //查询所有员工
    $scope.empList=empFactory.selectAll();

    $scope.updatePerEval=PEFactory.getPerEval();

    $scope.update=function(perEavl,empObj,res){
        console.log(perEavl)
        console.log(empObj)
        var perEvalObj={
            "id":$scope.updatePerEval.id,
            "employee.id":empObj.id,
            "date":perEavl.date,
            "task_target_name":perEavl.task_target_name,
            "task_target_num":perEavl.task_target_num,
            "actual_complete_num":perEavl.actual_complete_num,
            "actual_complete_rate":perEavl.actual_complete_rate,
            "keytask":perEavl.keytask,
            "actual_proce":perEavl.actual_proce,
            "person_devolop_target":perEavl.person_devolop_target,
            "person_review":perEavl.person_review,
            "person_advise":perEavl.person_advise,
            "org_manage_target":perEavl.org_manage_target,
            "org_review":perEavl.org_review,
            "org_advise":perEavl.org_advise,
            "reward_name":perEavl.reward_name,
            "reward_reason":perEavl.reward_reason,
            "cash_type":perEavl.cash_type,
            "punish_name":perEavl.punish_name,
            "punish_reason":perEavl.punish_reason,
            "other_chat":perEavl.other_chat,
            "onwork_pendate":perEavl.onwork_pendate,
            "next_pendate":perEavl.next_pendate,
            "deal_status":res.value
        }

        if(empObj.id!=null&&empObj.id!=""
            && perEavl.task_target_name!=null&&perEavl.task_target_name!=""
            && perEavl.actual_complete_num!=null&&perEavl.actual_complete_num!=""
            && perEavl.actual_complete_rate!=null&&perEavl.actual_complete_rate!=""
            && perEavl.keytask!=null&&perEavl.keytask!=""
            && perEavl.actual_proce!=null&&perEavl.actual_proce!=""
            && perEavl.onwork_pendate!=null&&perEavl.onwork_pendate!=""){
            PEFactory.updatePerEval(perEvalObj);
        }
    };

    $scope.results=[
        {name:"未处理",value:"0"},
        {name:"已完成",value:"1"}
    ];

    CommonUtils.setDatetimepicker("oneDatetimepicker");
    CommonUtils.setDatetimepicker("twoDatetimepicker");
    CommonUtils.setDatetimepicker("threeDatetimepicker");
});